package com.hongguan.oms.web.customer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.common.Const;
import com.hongguan.oms.domain.UserInfo;
import com.hongguan.oms.service.customer.CustomerService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;
import com.hongguan.oms.web.UserHandler;

public class CustomerHead extends BaseHandler {

	private CustomerService customerService;

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public ModelAndView updateCustomer(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {

		ModelAndView mv = null;
		mv = this.getSuccessModelView();

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();

			map.put("customerId", request.getParameter("customerId"));
			if (request.getParameter("CustomerName") != null) {
				map.put("CustomerName", request.getParameter("CustomerName"));
			}
			if (request.getParameter("customerArea") != null) {
				map.put("customerArea", request.getParameter("customerArea"));
			}
			if (request.getParameter("leader") != null) {
				map.put("leader", request.getParameter("leader"));
			}

			if (request.getParameter("CustomerAddress") != null) {
				map.put("CustomerAddress", request
						.getParameter("CustomerAddress"));
			}

			if (request.getParameter("COMPANYSIZE") != null) {
				map.put("COMPANYSIZE", request.getParameter("COMPANYSIZE"));
			}
			if (request.getParameter("FirstContact") != null) {
				map.put("FirstContact", request.getParameter("FirstContact"));
			}
			if (request.getParameter("CompanyPhone") != null) {
				map.put("CompanyPhone", request.getParameter("CompanyPhone"));
			}
			if (request.getParameter("Mobile") != null) {
				map.put("Mobile", request.getParameter("Mobile"));
			}
			if (request.getParameter("qq") != null) {
				map.put("qq", request.getParameter("qq"));
			}
			if (request.getParameter("msn") != null) {
				map.put("msn", request.getParameter("msn"));
			}
			if (request.getParameter("Email") != null) {
				map.put("Email", request.getParameter("Email"));
			}
			if (request.getParameter("fax") != null) {
				map.put("fax", request.getParameter("fax"));
			}
			if (request.getParameter("ContactLike") != null) {
				map.put("ContactLike", request.getParameter("ContactLike"));
			}

			if (request.getParameter("Birthday").equals("")) {
				map.put("Birthday", "1990-1-1");
			} else {
				map.put("Birthday", request.getParameter("Birthday"));
			}
			if (request.getParameter("SecondContact") != null) {
				map.put("SecondContact", request.getParameter("SecondContact"));
			}
			if (request.getParameter("SecondMobile") != null) {
				map.put("SecondMobile", request.getParameter("SecondMobile"));
			}
			if (request.getParameter("PhoneMoney") != null) {
				double PhoneMoney = Double.parseDouble(request
						.getParameter("PhoneMoney"));
				map.put("PhoneMoney", PhoneMoney);
			}
			if (request.getParameter("MailMoney") != null) {
				double MailMoney = Double.parseDouble(request
						.getParameter("MailMoney"));
				map.put("MailMoney", MailMoney);
			}
			if (request.getParameter("netMoney") != null) {
				double netMoney = Double.parseDouble(request
						.getParameter("netMoney"));
				map.put("netMoney", netMoney);
			}
			if (request.getParameter("SuccessTime").equals("")) {
			} else {
				map.put("SuccessTime", request.getParameter("SuccessTime"));
			}
			if (request.getParameter("SALESTAGE") != null) {
				map.put("SALESTAGE", request.getParameter("SALESTAGE"));
			}else{
				map.put("SALESTAGE", "0");
			}
			if (request.getParameter("VsName") != null) {
				map.put("VsName", request.getParameter("VsName"));
			}
			if (request.getParameter("VsManager") != null) {
				map.put("VsManager", request.getParameter("VsManager"));
			}
			if (request.getParameter("VsTime").equals("")) {
			} else
				map.put("VsTime", request.getParameter("VsTime"));

			if (request.getParameter("VsGood") != null) {
				map.put("VsGood", request.getParameter("VsGood"));
			}
			if (request.getParameter("VsBad") != null) {
				map.put("VsBad", request.getParameter("VsBad"));
			}

			map.put("user_Id", Util.getUserIdFromSession(request));
			map.put("updatedate", new SimpleDateFormat("yyyy-MM-dd")
					.format(date));
			map.put("Stage", request.getParameter("Stage"));

			customerService.updateCustomer(map);
			mv.addObject(SUCCESS, "true");
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}

		return mv;

	}

	public ModelAndView addCustomer(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		// ModelAndView mv = new ModelAndView();
		// mv = null;
		//mv = this.getSuccessModelView();
		ModelAndView mv = new ModelAndView("/chooseCustomer");
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Const.SESSION_USER);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();
			map.put("Stage", request.getParameter("Stage"));

			if (request.getParameter("CustomerName") != null) {
				map.put("CustomerName", request.getParameter("CustomerName"));
			}
			if (request.getParameter("customerArea") != null) {
				map.put("customerArea", request.getParameter("customerArea"));
			}
			if (request.getParameter("leader") != null) {
				map.put("leader", request.getParameter("leader"));
			}

			if (request.getParameter("CustomerAddress") != null) {
				map.put("CustomerAddress", request
						.getParameter("CustomerAddress"));
			}

			if (request.getParameter("COMPANYSIZE") != null) {
				map.put("COMPANYSIZE", request.getParameter("COMPANYSIZE"));
			}
			if (request.getParameter("FirstContact") != null) {
				map.put("FirstContact", request.getParameter("FirstContact"));
			}
			if (request.getParameter("CompanyPhone") != null) {
				map.put("CompanyPhone", request.getParameter("CompanyPhone"));
			}
			if (request.getParameter("Mobile") != null) {
				map.put("Mobile", request.getParameter("Mobile"));
			}
			if (request.getParameter("qq") != null) {
				map.put("qq", request.getParameter("qq"));
			}
			if (request.getParameter("msn") != null) {
				map.put("msn", request.getParameter("msn"));
			}
			if (request.getParameter("Email") != null) {
				map.put("Email", request.getParameter("Email"));
			}
			if (request.getParameter("fax") != null) {
				map.put("fax", request.getParameter("fax"));
			}
			if (request.getParameter("ContactLike") != null) {
				map.put("ContactLike", request.getParameter("ContactLike"));
			}

			if (request.getParameter("Birthday").equals("")) {
			} else {
				map.put("Birthday", request.getParameter("Birthday"));

			}

			if (request.getParameter("SecondContact") != null) {
				map.put("SecondContact", request.getParameter("SecondContact"));
			}
			if (request.getParameter("SecondMobile") != null) {
				map.put("SecondMobile", request.getParameter("SecondMobile"));
			}
			if (request.getParameter("PhoneMoney") != null) {
				double PhoneMoney = Double.parseDouble(request
						.getParameter("PhoneMoney"));
				map.put("PhoneMoney", PhoneMoney);
			}
			if (request.getParameter("MailMoney") != null) {
				double MailMoney = Double.parseDouble(request
						.getParameter("MailMoney"));
				map.put("MailMoney", MailMoney);
			}
			if (request.getParameter("netMoney") != null) {
				double netMoney = Double.parseDouble(request
						.getParameter("netMoney"));
				map.put("netMoney", netMoney);
			}
			if (request.getParameter("SuccessTime").equals("")) {
			} else {
				map.put("SuccessTime", request.getParameter("SuccessTime"));
			}
			if (request.getParameter("SALESTAGE") != null) {
				map.put("SALESTAGE", request.getParameter("SALESTAGE"));
			}else{
				map.put("SALESTAGE", "0");
			}
			if (request.getParameter("VsName") != null) {
				map.put("VsName", request.getParameter("VsName"));
			}
			if (request.getParameter("VsManager") != null) {
				map.put("VsManager", request.getParameter("VsManager"));
			}
			if (request.getParameter("VsTime").equals("")) {
			} else
				map.put("VsTime", request.getParameter("VsTime"));
			if (request.getParameter("VsGood") != null) {
				map.put("VsGood", request.getParameter("VsGood"));
			}
			if (request.getParameter("VsBad") != null) {
				map.put("VsBad", request.getParameter("VsBad"));
			}

			map.put("user_Id", Util.getUserIdFromSession(request));
			
			map.put("depart_Id", user.getBranchId());
			
			map.put("createdate", new SimpleDateFormat("yyyy-MM-dd")
					.format(date));

			customerService.addCustomer(map);
			mv.addObject(SUCCESS, "true");

		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}

		return mv;
	}

	public ModelAndView chooseCustomer(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = new ModelAndView("/chooseCustomer");
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			String customerId = request.getParameter("ids");
			String[] customerIdArray = customerId.split(",");
			UserInfo user = (UserInfo) request.getSession().getAttribute(
					Const.SESSION_USER);
			for (int i = 0; i < customerIdArray.length; i++) {
				map.put("stage", "1");
				map.put("customerId", customerIdArray[i]);
				map.put("user_Id", Util.getUserIdFromSession(request));
				map.put("depart_Id", user.getBranchId());
				customerService.chooseCustomer(map);
			}
			mv.addObject(SUCCESS, "true");
		} catch (Exception e) {
			mv.addObject(ERROR, "false");
		}
		return mv;
	}
	
	public ModelAndView giveUpCustomer(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = new ModelAndView("/chooseCustomer");
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			String customerId = request.getParameter("custID");
			
			UserInfo user = (UserInfo) request.getSession().getAttribute(
					Const.SESSION_USER);
			
				map.put("stage", "0");
				map.put("customerId",customerId);
				map.put("user_Id", Util.getUserIdFromSession(request));
				map.put("depart_Id", user.getBranchId());
				customerService.chooseCustomer(map);
			
			mv.addObject(SUCCESS, "true");
		} catch (Exception e) {
			mv.addObject(ERROR, "false");
		}
		return mv;
	}

	public ModelAndView searchCustomer(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		
		
		
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			String searchName = request.getParameter("searchName");
			String companysize = request.getParameter("companysize");
			String searchArea = request.getParameter("searchArea");
			String searchComplateDate = request
					.getParameter("searchComplateDate");
			String searchEndDate = request.getParameter("searchEndDate");

			if (searchName != null) {
				map.put("searchName", searchName);
			}
			if (companysize != null) {
				map.put("companysize", companysize);
			}

			if (searchArea != null) {
				map.put("searchArea", searchArea);
			}
			if (searchComplateDate != null) {
				map.put("searchComplateDate", searchComplateDate);
			}
			if (searchEndDate != null) {
				map.put("searchEndDate", searchEndDate);
			}
			String Stage = request.getParameter("stage");
			
			map.put("SaleStage",request.getParameter("SaleStage"));
			map.put("user_Id", Util.getUserIdFromSession(request));
			map.put("depart_Id", Util.getUserDepartmentFromSession(request));
			
			if ("0".equals(Stage)) {
				mv.addObject("result", customerService
						.searchCustomerByPublic(map));
				
			}
			if ("1".equals(Stage)) {
				mv.addObject("result", customerService
						.searchCustomerByPerson(map));
				
			}

			mv.addObject(SUCCESS, Boolean.TRUE);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
		return mv;

	}
}
